﻿using shuatibao.Tool;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace shuatibao.Question
{
    public class QuestionAction
    {
        #region 新增题目
        public static void insertQuestionManager(QuestionModel questionModel) {
            // 1. 创建雪花id
            string questionId = Utils.RandomString(15);

            // 连接数据库
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sqlcon.Open();
            string StrInsert = "";

            StrInsert = "insert into Question (type,question_name,answer_success,answer_analysis,subject_id,sub_subject_id,part_id,chapter_id,question_id) values (@type,@question_name,@answer_success,@answer_analysis,@subject_id,@sub_subject_id,@part_id,@chapter_id,@question_id)";
            SqlCommand cmd = new SqlCommand(StrInsert, sqlcon);
            // 添加参数并且设置参数值

            cmd.Parameters.Add("@type", SqlDbType.Int);
            cmd.Parameters["@type"].Value = questionModel.type;

            cmd.Parameters.Add("@question_name", SqlDbType.VarChar, 100);
            cmd.Parameters["@question_name"].Value = questionModel.question_name;


            cmd.Parameters.Add("@answer_success", SqlDbType.Char,10);
            cmd.Parameters["@answer_success"].Value = questionModel.answer_success == null ? "" : questionModel.answer_success;

            cmd.Parameters.Add("@answer_analysis", SqlDbType.Char);
            cmd.Parameters["@answer_analysis"].Value = questionModel.answer_analysis == null ? "" : questionModel.answer_analysis; ; 
            cmd.Parameters.Add("@subject_id", SqlDbType.Char);
            cmd.Parameters["@subject_id"].Value = questionModel.subject_id == null ? "" : questionModel.subject_id;
            cmd.Parameters.Add("@sub_subject_id", SqlDbType.Char);
            cmd.Parameters["@sub_subject_id"].Value = questionModel.sub_subject_id == null ? "" : questionModel.sub_subject_id;
       

            cmd.Parameters.Add("@question_id", SqlDbType.Char);
            cmd.Parameters["@question_id"].Value = questionId;

            // 执行插入数据的操作
            cmd.ExecuteNonQuery();
            sqlcon.Close();



            // 插入所有选项
            for (int i = 0; i < questionModel.optionsItems.Count; i++) {
                OptionsItem answerItem = questionModel.optionsItems[i];
                // 插入选项
                insertAnswerItem(answerItem, questionId);
            }

        }
        #endregion

        #region 插入选项
        public static void insertAnswerItem(OptionsItem answerItem,string questionId) {
            // 连接数据库
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sqlcon.Open();
            string StrInsert = "";

     
            StrInsert = "insert into Answer_Item (type,name,image,parent_id) values (@type,@name,@image,@parent_id)";
            SqlCommand cmd = new SqlCommand(StrInsert, sqlcon);
            // 添加参数并且设置参数值

            cmd.Parameters.Add("@type", SqlDbType.Int);
            cmd.Parameters["@type"].Value = answerItem.type;

            cmd.Parameters.Add("@name", SqlDbType.VarChar, 100);
            cmd.Parameters["@name"].Value = answerItem.name;


            cmd.Parameters.Add("@image", SqlDbType.Char);
            cmd.Parameters["@image"].Value = answerItem.image == null ? "" : answerItem.image; ;

            cmd.Parameters.Add("@parent_id", SqlDbType.Char);
            cmd.Parameters["@parent_id"].Value = questionId;

            // 执行插入数据的操作
            cmd.ExecuteNonQuery();
            sqlcon.Close();



        }
        #endregion



        #region 获取所有题目
        public static List<QuestionModel> getQuestionListManager(QuestionRequest questionRequest) {


            string baseSql = "SELECT * FROM Question WHERE 1 = 1";


            if (questionRequest.subjectId != null)
            {
                baseSql = baseSql + " and subject_id=" +"'" + questionRequest.subjectId + "'";
            }
            if (questionRequest.subSubjectId != null)
            {
                baseSql = baseSql + " and sub_subject_id=" + "'" + questionRequest.subSubjectId + "'";
            }

            if (questionRequest.pageSize != 0 && questionRequest.pageNumber != 0) {
                int pageWithIndex = ((questionRequest.pageNumber - 1) * questionRequest.pageSize);
                int maxPageIndex = pageWithIndex + questionRequest.pageSize;
                baseSql = baseSql + "ORDER BY id desc OFFSET " + pageWithIndex + " ROWS FETCH NEXT " + questionRequest.pageSize + "ROWS ONLY";

            }

            List<QuestionModel> orderList = new List<QuestionModel>();
            DataRowCollection dataRows = SQLTool.sqlGetListManager(baseSql);


            for (int i = 0; i < dataRows.Count; i++)
            {
                string id = dataRows[i]["id"].ToString();
                string questionId = dataRows[i]["question_id"].ToString();
                int type = Convert.ToInt32(dataRows[i]["type"].ToString());
                string question_name = dataRows[i]["question_name"].ToString();
                string answer_success = dataRows[i]["answer_success"].ToString();
                string answer_analysis = dataRows[i]["answer_analysis"].ToString();

                List<OptionsItem> optionsItems = getQuestionItemList(questionId);

                QuestionModel questionModel = new QuestionModel()
                {
                    id = id,
                    type = type,
                    question_name = question_name,
                    optionsItems  = optionsItems,
                    questionId= questionId,
                    answer_success = answer_success,
                    answer_analysis = answer_analysis,
                   
                };
                orderList.Add(questionModel);
            }

            return orderList;
        }
        #endregion

        #region 获取题目的所有选项
        public static List<OptionsItem> getQuestionItemList(string questionId) {
            List<OptionsItem> answerItemList = new List<OptionsItem>();


            string baseSql = "SELECT * FROM Option_Item WHERE parent_id = '" + questionId+"'";

            DataRowCollection dataRows = SQLTool.sqlGetListManager(baseSql);


            for (int i = 0; i < dataRows.Count; i++)
            {

                string id = dataRows[i]["id"].ToString();
                int type = Convert.ToInt32(dataRows[i]["type"].ToString());
                string name = dataRows[i]["name"].ToString();
                string image = dataRows[i]["image"].ToString();


                OptionsItem answerItem = new OptionsItem()
                {
                    id = id,
                    type = type,
                    name = name,
                    image = image

                };
                answerItemList.Add(answerItem);
            }

            return answerItemList;

        }

        #endregion

    }
}